<template>
    <div class="selectpp-auditor">
        <Select v-if="status=='e'" size="small" @on-change="changedmyeclass" placeholder="请选择班级">
            <Option v-for="(item,ind) in trees" :value="item.value" :key="ind">{{item.title}}</Option>
        </Select>

        <Cascader v-else :data="trees" size="small" @on-change="changed" :load-data="loadData" change-on-select placeholder="请选择年级"></Cascader>

        <div style="min-height:250px;margin-top:7px;height:400px;overflow:auto">
            <RadioGroup v-model="examine_user" @on-change="toselec">
                <div v-for="item in examiners" :key="item.user_id" style="margin-bottom:2px"><Radio :label="item.user_id">{{item.student_name.zh}}</Radio></div>
            </RadioGroup>
            <div style="text-align:center" v-if="Messagem">-----{{Message}}-----</div>
            <Spin size="large" fix v-if="spinShow"></Spin>
        </div>
    </div>
</template>

<script>
export default {
    props: {
        status: {
            type: [String],
            default: 's'
        }
    },
    data() {
        return {
            Message:'请选择学生',
            Messagem:true,
            spinShow:false,

            examine_user: '',
            examiners: [],
            // 菜单树
            trees: [],
            studenturl:null,
        }
    },
    methods: {

       
        /************************************************************
         * 
         * 加载年级
         * 
         ************************************************************/
        loadep() {
            this.$http.get('api/base/v1/get/grade/list')
            .then(res=>{  
                this.trees = this.process(res.data) 
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschoolnjlst'));
                }
            })
        },


        /************************************************************
         * 
         * 加载我的年级
         * 
         ************************************************************/
        loadepmy() {
            this.$http.get('api/student/v1/studentlearnstatus/my_grades')
            .then(res=>{  
                this.trees = this.process(res.data) 
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschoolnjlst'));
                }
            })
        },

        /************************************************************
         * 
         * 加载我的班级
         * 
         ************************************************************/
        loadepmyeclass() {
            this.$http.get('api/safety/v1/certifications/classlist')
            .then(res=>{  
                this.trees = this.process(res.data) 
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschoolbjlst'));
                }
            })
        },

        /************************************************************
         * 
         * 处理数据
         * 
         ************************************************************/
        process(data) {
            let arr = []
            data.forEach(item => {
                if(item.eclass_name===undefined){
                    item.title = item.label = item.grade_name.zh?item.grade_name.zh:item.grade_name.mn
                    item.loading = false
                    item.children = []
                    item.value = item.id
                }else{
                    item.title = item.label = item.eclass_name.zh?item.eclass_name.zh:item.eclass_name.mn
                    item.value = item.eclass_id
                }
                arr.push(item)
            })
            return arr
        },

        /************************************************************
         * 
         * 异步加载班级
         * 
         ************************************************************/
        loadData(item, callback) {
            item.loading = true;
            this.$http.get('api/eclass/v1/eclass/grade_eclass?grade_id='+item.id)
            .then(res=>{  
                item.children = this.process(res.data)
                item.loading = false;
                callback()        
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschoolbjlst'));
                }
            })
        },

        /************************************************************
         * 
         * 点击年级班级
         * 
         ************************************************************/
        changed(value) {
            if(value.length==1){
                //点击年级
                this.studenturl='api/student/v1/studentlearn/grade_studentname?grade_id='+value[0]
                this.studentfun()
            }
            if(value.length==2){
                //点击班级
                this.studenturl='api/student/v1/studentlearn/studentname?eclass_id='+value[1]
                this.studentfun()

            }
        },

        /************************************************************
         * 
         * 点击我的班级
         * 
         ************************************************************/
        changedmyeclass(value) {
            this.studenturl='api/student/v1/studentlearn/studentname?eclass_id='+value
            this.studentfun()
        },



        /************************************************************
         * 
         * 点击年级班级获取学生姓名
         * 
         ************************************************************/
        studentfun() {
            this.spinShow=true 
            this.$http.get(this.studenturl)
            .then(res=>{  
                this.examiners=[]
                res.data.forEach(item => {
                  if(item.studentbase!==null){
                    this.examiners.push(item.studentbase)
                  }
                })
                if(this.examiners.length==0){
                    this.Message='暂无数据',
                    this.Messagem=true
                }else{
                    this.Messagem=false
                }
                this.spinShow=false       
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschoolxslst'));
                }
                this.spinShow=false 
            })
        },


        toselec(val) {
            let str = this.examiners.filter(item => item.user_id == val)
            let tp={}
            tp.value=val
            tp.student_base_id=str[0].student_base_id
            tp.sex=str[0].sex
            tp.lable=str[0].student_name.zh
            this.$emit('on-select',tp)
        }
    },
    created() {
        if(this.status=='s'){
            this.loadep()
        }
        if(this.status=='g'){
            this.loadepmy()
        }
        if(this.status=='e'){
            this.loadepmyeclass()
        }
    }
}
</script>

<style>

</style>
